#include <iostream>
#include <cmath>
using namespace std;
long long t, n, len;
long long arr[25], brr[25], ans;
int main(){
	for (long long i = 1; i < 1e10; i *= 3){
		arr[len++] = i;
	}
	for (int i = 0; i < len; i++){
		brr[i] = pow(3,i + 1) + i * pow(3,i - 1);
	}
	cin >> t;
	 while(t--){
	 	ans = 0;
	 	cin >> n;
	 	while(n){
	 		for (int i = 0; i < len; i++){
	 			if (arr[i] > n){
	 				long long sum = n / arr[i - 1];
	 				ans += brr[i - 1] * sum;
	 				n = n - sum * arr[i - 1];
	 				break;
				 }
			 }
		 }
		 cout << ans << endl;
	 }
	return 0;
}
